<?php
    include_once("inc/conn.php");
    include_once("inc/function.php");
    
    if(!isset($_SESSION['usersid'])) {
		echo "您还没登录，请先去<a href='/login.php'>登录</a>!";
		exit;
	}
	$act = intval($_GET['act']);
	//更新银行
	RefreshPoints();
	//返回数据
	GetGameData();
    
    /* 取得游戏刷新数据
    * 
    */
    function GetGameData()
    {
    	global $db;
    	
		//取得子菜单
		$act = intval($_GET['act']);
		$sid = intval($_GET['sid']);
		$page = isset($_GET['page'])?$_GET['page']:1;
		$page =intval($page);
		$pagesize = 20;
		
		$arrCurNoInfo = array('preno'=>'','prekgtime'=>'','game_kj_delay'=>'','game_tz_close'=>'');
		$RetContent = "<div class='Pattern'>\r\n";
		$RetContent .= "\t<div class='Content'>\r\n";
		
		//开奖头部
		//$RetContent .= GetHeadContent($act,$sid,$arrCurNoInfo);
		$RetContent .= get_yj_head_content($act,$sid,$arrCurNoInfo);
		//游戏菜单
		$js=GetRewardJS($act,$arrCurNoInfo,"game");

		//取表格
		$RetContent .= GetTableContent($act,$page,$pagesize,$arrCurNoInfo);
		
		$RetContent .= "\t</div>\r\n";
		$RetContent .= "</div>\r\n";
		//js 定义
		$RetContent .= $js;
		$RetContent .= GetJSContent($act);
		echo $RetContent;
		exit;
    }
    
    function GetTableContent($act,$page,$pagesize,$arrnoinfo)
	{
		global $db;
		$tablegame = GetGameTableName($act,"game");
		$tablegametz = GetGameTableName($act,"users_tz");
		$tablegamekg = GetGameTableName($act,"kg_users_tz");
		$MinuteAdd = 20; //北京数据源
		if($act == "0" || $act == "1" || $act == "2" || $act == "15") //急速
			$MinuteAdd = "4";
		else if($act == "8" || $act == "9" || $act == "10" || $act == "13" || $act == "24")//加拿大源
			$MinuteAdd = "13";
		else if($act == "18" || $act == "19" || $act == "20" || $act == "21"|| $act == "23")//韩国源
			$MinuteAdd = "6";
		
		$sql = "SELECT count(id) FROM {$tablegame} WHERE kgtime < DATE_ADD(NOW(),INTERVAL {$MinuteAdd} MINUTE) ORDER BY id desc";
		$TotalRecCount = $db->GetRecordCount($sql);
		$sql = "
		 SELECT a.id,a.kgtime,now() as nowtime,a.kj,a.kgjg,a.kgNo,a.tzpoints,a.tznum,a.zjrnum,IFNULL(b.tzpoints,0) AS ptzpoints,IFNULL(b.hdpoints,0) AS phdpoints
		 FROM 
		 (
			 SELECT id,kgtime,kj,kgjg,kgNo,tzpoints,(zdtz+zdtz_r+sdtz) as tznum,zjrnum
			 FROM {$tablegame} 
			 WHERE  kgtime < DATE_ADD(NOW(),INTERVAL {$MinuteAdd} MINUTE) 
		 ) AS a
		 LEFT OUTER JOIN
		 (
			 SELECT NO,SUM(tzpoints) tzpoints,SUM(hdpoints) hdpoints
			 FROM {$tablegamekg}
			 WHERE uid = '{$_SESSION['usersid']}' 
			 GROUP BY NO
			 UNION
			 SELECT NO,points AS tzpoints,hdpoints 
			 FROM {$tablegametz}
			 WHERE uid = '{$_SESSION['usersid']}'
		 ) AS b
		 ON a.id = b.no
		 ORDER BY id desc ";
		$sql .= GetLimit($page,$pagesize);
		
		if ( $act == "24" || $act == "23" )  
		{
			$kjj='开奖号码';
			$kjj1='开奖结果';
			$kjj2='';
		}
		else
		{
			$kjj='开奖结果';
			$kjj1='开奖结果';$kjj2="style='display: none'";}
		
			$result =  $db->query($sql);
			$divTable = "<div class='table'>\r\n";
			$divTable .= "\t<table class='table_list table table-striped table-bordered table-hover' cellspacing='0px' style='border-collapse:collapse;'>\r\n";
			$divTable .= "\t<tbody>\r\n";
			$divTable .= "\t\t<tr>\r\n";
			$divTable .= "\t\t\t<th width='65'>期 号</th>\r\n";
			$divTable .= "\t\t\t<th width='116'>开奖时间</th>\r\n";
			$divTable .= "\t\t\t<th width='283'>{$kjj}</th>\r\n";
			$divTable .= "\t\t\t<th width='100' {$kjj2}>{$kjj1}</th>\r\n";
			$divTable .= "\t\t\t<th width='146'>宝币总数</th>\r\n";
			$divTable .= "\t\t\t<th width='100'>中奖人数</th>\r\n";
			$divTable .= "\t\t\t<th width='160'>投注额/中奖额</th>\r\n";
			$divTable .= "\t\t\t<th width='120'>投注</th>\r\n";
			$divTable .= "\t\t</tr>\r\n";
			while(!!$rs=$db->fetch_array($result))
			{
				$arrTmpKg = explode("|",$rs["kgjg"]);
				$Winner = "{$rs['zjrnum']}/{$rs['tznum']}";
			if($rs['kj'] == 0)
			{
				if( $arrnoinfo['prekgtime'] <= ($arrnoinfo['game_tz_close']) && $rs['id'] == $arrnoinfo['preno'] ) //正在开奖
				{
					$TmpState = "<span class='btn btn-warning btn-block'>开奖中...</span>";
				}
				else
				{
					
					if(DateDiff($rs["kgtime"],$rs["nowtime"],"s") > 0)
					{
						$TmpState = "<a href='javascript:toPress({$rs['id']});'><span class='btn btn-danger btn-block'>立即投注</span></a>";
					}
					else
					{
						
						$TmpState = "<span class='btn btn-warning btn-block'>开奖中...</span>";
					}
				}
				$TmpKaiNum = "";
			}
			else
			{
				if ($rs['zjrnum'] == 0){
					$TmpState = "<a id='state_{$rs['id']}' class='btn btn-default btn-block'>已开奖</a>";
				}else{
					$TmpState = "<a id='state_{$rs['id']}' class='btn btn-default btn-block' href=\"javascript:showrecord('{$rs['id']}','srecdetail.php?act={$act}&no=".$rs['id']."');\">已开奖</a>";
				}

				if($act == "0" || $act == "1" || $act == "2" || $act == "3" || $act == "4" || $act == "5" ||
				   $act == "8" || $act == "9" || $act == "10" || $act == "11" || $act == "12" || $act == "13" ||
				   $act == "18" || $act == "19" || $act == "20" || $act == "21"|| $act == "22" || $act == "23"
				  ) //快乐8类
				{
					$kjNumberStr = ($arrTmpKg[2] == "-1") ? (show_num($arrTmpKg[0],1)."<i class='hja'></i>".show_num($arrTmpKg[1],1) ) : ( show_num($arrTmpKg[0],1)."<i class='hja'></i>".show_num($arrTmpKg[1],1)."<i class='hja'></i>".show_num($arrTmpKg[2],1)  );
					$kjFinal = $arrTmpKg[3];
					//$value_kj=" = <em class='finals final'><i>{$kjFinal}</i></em>";
					$value_kj="<i class='hdeng'></i>".show_num($arrTmpKg[3],3);
					if($act == "11" || $act == "12" || $act == "13" || $act == "21") //36游戏
					{
						switch($arrTmpKg[3])
						{
							case 1:
								$kjFinal = "豹";
								$value_kj="<i class='hdeng'></i>".show_num(1,2);
								break;
							case 2:
								$kjFinal = "对";
								$value_kj="<i class='hdeng'></i>".show_num(2,2);
								break;
							case 3:
								$kjFinal = "顺";
								$value_kj="<i class='hdeng'></i>".show_num(3,2);
								break;
							case 4:
								$kjFinal = "半";
								$value_kj="<i class='hdeng'></i>".show_num(4,2);
								break;
							case 5:
								$kjFinal = "杂";
								$value_kj="<i class='hdeng'></i>".show_num(5,2);
								break;
							default:
								$kjFinal = "";
								break;
						}
					}
					//$TmpKaiNum = "{$kjNumberStr} = <em class='finals final'><i>{$kjFinal}</i></em>";
					$TmpKaiNum = "{$kjNumberStr}{$value_kj}";
				}
				else if($act == "23" || $act == "24" ) //韩国10，加拿大10
				{
					//取得1-10个开奖结果排列
					$arrKJResult = explode("|",$rs["kgNo"]);
					$TmpKaiNum = "";
					$kjNumPrefix = "";
					for($i = 1; $i <= count($arrKJResult);$i++)
					{
						$kjNumPrefix = "light";
						switch($act)
						{
							case "6": //pk10
								$TmpSuffixNum = $arrTmpKg[0];
								
								if($arrTmpKg[0] == 0) 
									$TmpSuffixNum = 10;
															
								if($i == $TmpSuffixNum)
									$kjNumPrefix = "regular";
								break;
							case "7": //pk冠军
								if($i == 1)
									$kjNumPrefix = "regular";
								break;
							case "14": //pk22
								if($i <= 3)
									$kjNumPrefix = "regular"; 
								break;
							case "15": //急速10
								if($i == 1)
									$kjNumPrefix = "regular";
								break;
							case "16": //pk龙虎
								if($i == 1 || $i == 10)
									$kjNumPrefix = "regular";
								break;
							case "23": //pk冠亚军
								if($i == 1 || $i == 2)
									$kjNumPrefix = "regular";
								break;
							default:
								break;
						}
						$theKjNumber = substr("0" . $arrKJResult[$i-1],-2); 
						$TmpKaiNum .= "<em class='{$kjNumPrefix}{$theKjNumber}'></em>";
						
					} }
				else if($act == "6" || $act == "7" || $act == "14" || $act == "15" || $act == "16" || $act == "17") //pk类，PK10、PK冠军、PK22、急速10、PK龙虎、PK冠亚军
				{
					//取得1-10个开奖结果排列
					$arrKJResult = explode("|",$rs["kgNo"]);
					$TmpKaiNum = "";
					$kjNumPrefix = "";
					for($i = 1; $i <= count($arrKJResult);$i++)
					{
						$kjNumPrefix = "light";
						switch($act)
						{
							case "6": //pk10
								$TmpSuffixNum = $arrTmpKg[0];
								
								if($arrTmpKg[0] == 0) 
									$TmpSuffixNum = 10;
															
								if($i == $TmpSuffixNum)
									$kjNumPrefix = "regular";
								break;
							case "7": //pk冠军
								if($i == 1)
									$kjNumPrefix = "regular";
								break;
							case "14": //pk22
								if($i <= 3)
									$kjNumPrefix = "regular"; 
								break;
							case "15": //急速10
								if($i == 1)
									$kjNumPrefix = "regular";
								break;
							case "16": //pk龙虎
								if($i == 1 || $i == 10)
									$kjNumPrefix = "regular";
								break;
							case "17": //pk冠亚军
								if($i == 1 || $i == 2)
									$kjNumPrefix = "regular";
								break;
							default:
								break;
						}
						$theKjNumber = substr("0" . $arrKJResult[$i-1],-2); 
						$TmpKaiNum .= "<em class='{$kjNumPrefix}{$theKjNumber}'></em>";
						
					} 
					//取得最后开奖结果 
					//$LastKGResult = "<em class='final'><i>{$arrTmpKg[3]}</i></em>";		急速10后面的开奖结果.
					//$LastKGResult = show_num($arrTmpKg[3],3);

					if($act == "16") //pk龙虎单独处理
					{
						if($arrTmpKg[3] == 1) //龙
						{
							//$LastKGResult = "<em class='final'><i>龙</i></em>";
							$LastKGResult = "<i class='lh n1'></i>";
						}
						else //虎
						{
							//$LastKGResult = "<em class='final'><i>虎</i></em>";
							$LastKGResult = "<i class='lh n2'></i>";
						} 
					}
					$TmpKaiNum .= $LastKGResult;
				}
			}
			$divTable .= "\t\t<tr>\r\n";
			
			$divTable .= "\t\t\t<td>{$rs['id']}</td>\r\n";
			$divTable .= "\t\t\t<td>" . date("m-d H:i:s",strtotime($rs["kgtime"])) . "</td>\r\n";
			if ($rs['kj'] == 0  || $act == 2 || $act == 1 || $act == 0|| $act == 16 ){
				$divTable .= "\t\t\t<td class='regular' valign='middle'>{$TmpKaiNum} </td>\r\n";
			}
			elseif ( $act ==6  ){
				
                $pk10kai=explode("|",$rs["kgNo"]);
				//$npk10=$pk10kai[$TmpSuffixNum];
				$qihao=$TmpSuffixNum-1;
                $npk10=$pk10kai[$qihao];
				$pk10="\t\t\t<i class='mh1'><span class='jg-wrap'>{$npk10}</span></i>\r\n";
				$divTable .= "\t\t\t<td class='regular' valign='middle'>{$TmpKaiNum}{$pk10}</span></i>　</td>\r\n";
			}
			elseif ( $act ==14  ){
				
                $pk10kai=explode("|",$rs["kgNo"]);
				//$npk10=$pk10kai[$TmpSuffixNum];
				$haoma1=$pk10kai[0];
				$haoma2=$pk10kai[1];
				$haoma3=$pk10kai[2];
				$zongkai=$haoma1+$haoma2+$haoma3;
				$pk10="\t\t\t<i class='mh1'><span class='jg-wrap'>{$zongkai}</span></i>\r\n";
				$divTable .= "\t\t\t<td class='regular' valign='middle'>{$TmpKaiNum}{$pk10}</span></i>　</td>\r\n";
			}
				elseif ( $act ==7 ||$act ==15 ){
				
                $pk10kai=explode("|",$rs["kgNo"]);
				//$npk10=$pk10kai[$TmpSuffixNum];
				$haoma1=$pk10kai[0];
				
				$pk10="\t\t\t<i class='mh1'><span class='jg-wrap'>{$haoma1}</span></i>\r\n";
				$divTable .= "\t\t\t<td class='regular' valign='middle'>{$TmpKaiNum}{$pk10}</span></i>　</td>\r\n";
			}
			
			elseif ( $act ==17  ){
				
                $pk10kai=explode("|",$rs["kgNo"]);
				//$npk10=$pk10kai[$TmpSuffixNum];
				$haoma1=$pk10kai[0];
				$haoma2=$pk10kai[1];
				//$haoma3=$pk10kai[2];
				$zongkai=$haoma1+$haoma2;
				$pk10="\t\t\t<i class='mh1'><span class='jg-wrap'>{$zongkai}</span></i>\r\n";
				$divTable .= "\t\t\t<td class='regular' valign='middle'>{$TmpKaiNum}{$pk10}</span></i>　</td>\r\n";
			}
			elseif ( $act ==23  ){
				
                $pk10kai=explode("|",$rs["kgNo"]);
				//$npk10=$pk10kai[$TmpSuffixNum];
				$hg10kai=substr($pk10kai, 0, 6);
				//$haoma3=$pk10kai[2];
				$pk10jie=$pk10kai[0].','.$pk10kai[1].','.$pk10kai[2].','.$pk10kai[3].','.$pk10kai[4].','.$pk10kai[5].','.$pk10kai[6];
				$zonghanguo=$pk10kai[0]+$pk10kai[1]+$pk10kai[2]+$pk10kai[3]+$pk10kai[4]+$pk10kai[5]+$pk10kai[6];
				$zongkai=substr($zonghanguo, -1 )+1;
				$pk10="\t\t\t<i class='mh1'><span class='jg-wrap'>{$zongkai}</span></i>\r\n";
				$divTable .= "\t\t\t<td class='regular' valign='middle'>{$pk10jie}</span></i>　</td>\r\n";
				$divTable .= "\t\t\t<td class='regular' valign='middle'>{$pk10}</span></i>　</td>\r\n";
			}
				elseif ( $act ==24  ){
				
                $pk10kai=explode("|",$rs["kgNo"]);
				//$npk10=$pk10kai[$TmpSuffixNum];
				$hg10kai=substr($pk10kai, 0, 6);
				//$haoma3=$pk10kai[2];
				$pk10jie=$pk10kai[0].','.$pk10kai[1].','.$pk10kai[2].','.$pk10kai[3].','.$pk10kai[4].','.$pk10kai[5].','.$pk10kai[6];
				$zonghanguo=$pk10kai[0]+$pk10kai[1]+$pk10kai[2]+$pk10kai[3]+$pk10kai[4]+$pk10kai[5]+$pk10kai[6];
				$zongkai=substr($zonghanguo, -1 )+1;
				$pk10="\t\t\t<i class='mh1'><span class='jg-wrap'>{$zongkai}</span></i>\r\n";
				$divTable .= "\t\t\t<td class='regular' valign='middle'>{$pk10jie}</span></i>　</td>\r\n";
				$divTable .= "\t\t\t<td class='regular' valign='middle'>{$pk10}</span></i>　</td>\r\n";
			}
			
			else{
				$divTable .= "\t\t\t<td class='regular' valign='middle'>{$TmpKaiNum}　<a href=\"javascript:openrecord('{$rs['id']}',840,400,'sgame_open_recode.php?act={$act}&id={$rs['id']}');\" class='btn btn-danger validate' style='outline: medium;' >验证</a></td>\r\n";
			}
          
				if($rs['kj'] == 0&&$act ==24){$divTable .= "\t\t\t<td> </td>\r\n";}
				else if($rs['kj'] == 0&&$act ==23){$divTable .= "\t\t\t<td> </td>\r\n";}
				else{}
			$divTable .= "\t\t\t<td class='please'><span>". Trans($rs['tzpoints']) ."</span></td>\r\n";
			$divTable .= "\t\t\t<td>{$Winner}</td>\r\n";
			if($rs['ptzpoints'] == 0)
				$winPoint = "<span>{$rs['ptzpoints']}/{$rs['phdpoints']}</span>";
			else if($rs['phdpoints'] - $rs['ptzpoints'] >= 0)
				$winPoint = "<a title='查看投注记录' href=\"javascript:getContent('sgamerecord.php?act={$act}&sid=3')\"><span style='color:red'>". $rs['ptzpoints'] ."/". $rs['phdpoints'] ."</span></a>";
			else
				$winPoint = "<a title='查看投注记录' href=\"javascript:getContent('sgamerecord.php?act={$act}&sid=3')\"><span style='color:blue'>". $rs['ptzpoints'] ."/". $rs['phdpoints'] ."</span></a>";
				
			$divTable .= "\t\t\t<td class='please'>{$winPoint}</td>\r\n";
			$divTable .= "\t\t\t<td class='state'><span id='scur_{$rs['id']}'>{$TmpState}</span></td>\r\n";
			$divTable .= "\t\t</tr>\r\n";
		}
		$divTable .= "\t</tbody>\r\n";
		$divTable .= "\t</table>\r\n";
		$RetContent .= $divTable;
		
		//分页
		if($TotalRecCount > 20)
		{
			$divPage .= "<div class='Paging'>\r\n";
			require_once('inc/fenye.php');
			$ajaxpage=new page(array('total'=>$TotalRecCount,'perpage'=>$pagesize,'ajax'=>"ajax_page",'nowindex' => $page));
			$divPage .= $ajaxpage->show();
			$divPage .= "</div>\r\n";
			$RetContent .= $divPage;
		}
		 
		$RetContent .= "</div>\r\n";  
		return $RetContent;
	}
    
    function GetJSContent($act)
    {
		$js = "<script type=\"text/javascript\">\r\n";
		$js .= "
		$(document).ready(function(){
				var issond = (getCookie('issond') == '')?1:getCookie('issond');
				if(issond==1){
					 $('#sond_offon').attr('src', '/images/S_Open.gif');
				}else{
					$('#sond_offon').attr('src', '/images/S_Close.gif');
				}
		
				if(issond==1){

				}
			
			});
			function ajax_page(page)
			{
				getContent('sgame.php?act={$act}&page=' + page + '&t=' +  Math.random());
			}
			function toPress(no)
			{
			   $.post('sgameservice.php?t=' + Math.random(),{act:'checkpress',gtype:{$act},no:no},function(ret){
			   		switch(ret.cmd)
			   		{
			   			case 'ok':
			   				getContent('spress.php?act={$act}&no=' + no);
			   				break;
			   			case 'auto':
			   				if(confirm('您已经设置了自动投注，请先关闭自动投注!')){
			   				 	getContent('sautopress.php?act={$act}' + '&no=' + no);
			   				}
			   				break;
			   			case 'shutdown':
			   				if(confirm(ret.msg)){
			   				 	getContent('sautopress.php?act={$act}' + '&no=' + no);
			   				}
			   				break;
			   			case 'yj_err':
			   				if(confirm('完善收款信息才能投注，前往完善？')){
			   				 	self.location = 'member.php?yj_act=bind_account';
			   				}	
			   				break;
			   			default:
			   				alert(ret.msg);
			   				break;
			   		}
			   },'json');
			}
				
		";
		
		$js .= "</script>\r\n";
		return $js;
    }
    //显示中奖号码
    function show_num($num,$type){
        if($type==1){
            return "<i class='kj kj_".$num."'>".$num."</i>";
        }
		if($type==2){
            return "<i class='zh z".$num."'></i>";
        }
        if($type==3){
             return "<i class='mh'><span class='jg-wrap'>$num</span></i>";
            //return "<i class=\"number number_{$num}\"></i>";
        }
    }
    
